﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BasePlatformNet;
using BasePlatformCommon;

namespace L9SubSet
{
	public partial class Form_DmxCurrentPro : Form
	{
		private BaseCoreNet baseCoreNet = null;

		private int devIndex = -1;
		private int subIndex = 0;
		private int portIndex = 0;
		private int miniIndex = -1;

		private int[] currentValuePro = new int[24];	// 电流参数 - 4*6=24

		private string section = "DmxCurPro";			// 多语言文件配置节名称

		private string tip = "提示";						// MessageBox 题头

		/*****************************************************************************************/

		// 构造函数
		public Form_DmxCurrentPro(BaseCoreNet baseCoreNet)
		{
			InitializeComponent();

			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
			this.Font = new System.Drawing.Font(this.Font.Name, 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel, ((byte)(134)));

			this.baseCoreNet = baseCoreNet;

			initEnvironment();
		}

		// 初始化运行环境
		private void initEnvironment()
		{
			for (int i = 0; i < 24; i++)
			{
				currentValuePro[i] = 100;
			}

			updateCurrentValuePro();

			proTotalCurrentFactorTextBox.Text = "100";
			
			currentSaveChannelComboBox.Items.Add("4");
			currentSaveChannelComboBox.Items.Add("8");
			currentSaveChannelComboBox.Items.Add("12");
			currentSaveChannelComboBox.Items.Add("16");
			currentSaveChannelComboBox.Items.Add("20");
			currentSaveChannelComboBox.Items.Add("24");
			currentSaveChannelComboBox.SelectedIndex = 5;

			// 设置本地化语言
			if (LanguageOper.available == true)
			{
				setLanguageText();
			}
		}

		// 设置本地化语言
		public void setLanguageText()
		{
			if (LanguageOper.available == false)
			{
				return;
			}

			LanguageOper.SetCurrentCulture();

			tip = LanguageOper.ReadValue("Common", "tipStr");

			// 界面控件文字
			this.Text = LanguageOper.ReadValue(section, "dmxCurPro.Text");
			noticeLabel.Text = LanguageOper.ReadValue(section, "noticeLabel.Text");
			string rStr = "     " + LanguageOper.ReadValue(section, "rLabel.Text");
			string gStr = "     " + LanguageOper.ReadValue(section, "gLabel.Text");
			string bStr = "     " + LanguageOper.ReadValue(section, "bLabel.Text");
			string wStr = "     " + LanguageOper.ReadValue(section, "wLabel.Text");
			if (LanguageOper.LocalLanguage != LanguageEnum.EN)
			{
				rStr = rStr.Trim();
				gStr = gStr.Trim();
				bStr = bStr.Trim();
				wStr = wStr.Trim();
			}
			rLabel1.Text = rStr;
			rLabel2.Text = rStr;
			rLabel3.Text = rStr;
			rLabel4.Text = rStr;
			rLabel5.Text = rStr;
			rLabel6.Text = rStr;
			gLabel1.Text = gStr;
			gLabel2.Text = gStr;
			gLabel3.Text = gStr;
			gLabel4.Text = gStr;
			gLabel5.Text = gStr;
			gLabel6.Text = gStr;
			bLabel1.Text = bStr;
			bLabel2.Text = bStr;
			bLabel3.Text = bStr;
			bLabel4.Text = bStr;
			bLabel5.Text = bStr;
			bLabel6.Text = bStr;
			wLabel1.Text = wStr;
			wLabel2.Text = wStr;
			wLabel3.Text = wStr;
			wLabel4.Text = wStr;
			wLabel5.Text = wStr;
			wLabel6.Text = wStr;
			proSetMaxCurrentButton.Text = LanguageOper.ReadValue(section, "proSetMaxCurrentButton.Text");
			allLabel.Text = LanguageOper.ReadValue(section, "allLabel.Text");
			channelLabel.Text = LanguageOper.ReadValue(section, "channelLabel.Text");
			proReadCurrentButton.Text = LanguageOper.ReadValue(section, "proReadCurrentButton.Text");
			proSaveCurrentButton.Text = LanguageOper.ReadValue(section, "proSaveCurrentButton.Text");
			proChangeByCurrentFactorButton.Text = LanguageOper.ReadValue(section, "proChangeByCurrentFactorButton.Text");
			closeButton.Text = LanguageOper.ReadValue(section, "closeButton.Text");

			if (LanguageOper.LocalLanguage != LanguageEnum.EN)
			{
				noticeLabel.Location = new Point(334, 14);
				channelLabel.Location = new Point(143, 162);
			}
			else
			{
				noticeLabel.Location = new Point(252, 14);
				channelLabel.Location = new Point(138, 162);
			}
		}

		// 父窗体传递参数
		public void setParentParameters(int devIndex, int subIndex, int portIndex, int miniIndex)
		{
			this.devIndex = devIndex;
			this.subIndex = subIndex;
			this.portIndex = portIndex;
			this.miniIndex = miniIndex;

			string subIndexStr = subIndex == 0 ? "All" : subIndex.ToString();
			string portIndexStr = portIndex == 0 ? "All" : portIndex.ToString();
			string miniIndexStr = miniIndex == -1 ? "All" : miniIndex.ToString();

			if (subIndex == 0 || portIndex == 0 || miniIndex == -1)
			{
				proReadCurrentButton.Enabled = false;
			}
			else
			{
				proReadCurrentButton.Enabled = true;
			}

			string msg = string.Format("操作目标：分控 {0}, 端口 {1}, 灯具 {2}", subIndexStr, portIndexStr, miniIndexStr);
			if (LanguageOper.available == true)
			{
				msg = string.Format(LanguageOper.ReadValue(section, "str.0"), subIndexStr, portIndexStr, miniIndexStr);
			}
			string targetInfo = string.Format(msg, subIndexStr, portIndexStr, miniIndexStr);

			targetLabel.Text = targetInfo;
		}

		// 将内部电流值更新到 UI 界面 - 高级
		private void updateCurrentValuePro()
		{
			Dot1RTextBox.Text = currentValuePro[0].ToString();
			Dot1GTextBox.Text = currentValuePro[1].ToString();
			Dot1BTextBox.Text = currentValuePro[2].ToString();
			Dot1WTextBox.Text = currentValuePro[3].ToString();

			Dot2RTextBox.Text = currentValuePro[4].ToString();
			Dot2GTextBox.Text = currentValuePro[5].ToString();
			Dot2BTextBox.Text = currentValuePro[6].ToString();
			Dot2WTextBox.Text = currentValuePro[7].ToString();

			Dot3RTextBox.Text = currentValuePro[8].ToString();
			Dot3GTextBox.Text = currentValuePro[9].ToString();
			Dot3BTextBox.Text = currentValuePro[10].ToString();
			Dot3WTextBox.Text = currentValuePro[11].ToString();

			Dot4RTextBox.Text = currentValuePro[12].ToString();
			Dot4GTextBox.Text = currentValuePro[13].ToString();
			Dot4BTextBox.Text = currentValuePro[14].ToString();
			Dot4WTextBox.Text = currentValuePro[15].ToString();

			Dot5RTextBox.Text = currentValuePro[16].ToString();
			Dot5GTextBox.Text = currentValuePro[17].ToString();
			Dot5BTextBox.Text = currentValuePro[18].ToString();
			Dot5WTextBox.Text = currentValuePro[19].ToString();

			Dot6RTextBox.Text = currentValuePro[20].ToString();
			Dot6GTextBox.Text = currentValuePro[21].ToString();
			Dot6BTextBox.Text = currentValuePro[22].ToString();
			Dot6WTextBox.Text = currentValuePro[23].ToString();
		}

		// 电流配置 - 高级 - 设为最大
		private void proSetMaxCurrentButton_Click(object sender, EventArgs e)
		{
			int channelNum = int.Parse(currentSaveChannelComboBox.Text);
			for (int i = 0; i < channelNum; i++)
			{
				currentValuePro[i] = 100;
			}
			updateCurrentValuePro();
		}

		// 电流配置 - 高级 - 整体调整
		private void proChangeByCurrentFactorButton_Click(object sender, EventArgs e)
		{
			int factor = 0;
			bool parseRes = int.TryParse(proTotalCurrentFactorTextBox.Text.Trim(), out factor);
			if (parseRes == false || factor < 0 || factor > 200)
			{
				string msg = "调整因子解析错误, 应为 [0,200] 之间的整数";
				if (LanguageOper.available == true)
				{
					msg = LanguageOper.ReadValue(section, "msgStr.0");
				}
			
				MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.Information);
				
				return;
			}
			else
			{
				int channelNum = int.Parse(currentSaveChannelComboBox.Text);
				for (int i = 0; i < channelNum; i++)
				{
					currentValuePro[i] = currentValuePro[i] * factor / 100;
					currentValuePro[i] = currentValuePro[i] > 100 ? 100 : currentValuePro[i];
				}
				updateCurrentValuePro();
			}
		}

		// 电流配置 - 高级 - 读取
		private void proReadCurrentButton_Click(object sender, EventArgs e)
		{
			byte[] cmdData = new byte[256];
			cmdData[2] = 0x70;
			int cmdCommNum = baseCoreNet.sendMiniCmd(devIndex, subIndex, portIndex, miniIndex, cmdData, 12);

			byte[] ackData = new byte[256];
			bool success = baseCoreNet.getMiniAck(devIndex, subIndex, portIndex, ackData, cmdCommNum);
			if (success == true)
			{
				int channelNum = int.Parse(currentSaveChannelComboBox.Text);
				for (int i = 0; i < channelNum; i++)
				{
					currentValuePro[i] = ackData[8 + i];
					currentValuePro[i] = currentValuePro[i] > 100 ? 100 : currentValuePro[i];
				}
				updateCurrentValuePro();
			}
			else
			{
				string msg = "电流参数读取错误";
				if (LanguageOper.available == true)
				{
					msg = LanguageOper.ReadValue(section, "msgStr.1");
				}
				
				MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.Information);
			}
		}

		// 电流配置 - 高级 - 保存
		private void proSaveCurrentButton_Click(object sender, EventArgs e)
		{
			try
			{
				int[] currentValuePro = new int[24];

				currentValuePro[0] = int.Parse(Dot1RTextBox.Text);
				currentValuePro[1] = int.Parse(Dot1GTextBox.Text);
				currentValuePro[2] = int.Parse(Dot1BTextBox.Text);
				currentValuePro[3] = int.Parse(Dot1WTextBox.Text);

				if (currentSaveChannelComboBox.SelectedIndex > 0)
				{
					currentValuePro[4] = int.Parse(Dot2RTextBox.Text);
					currentValuePro[5] = int.Parse(Dot2GTextBox.Text);
					currentValuePro[6] = int.Parse(Dot2BTextBox.Text);
					currentValuePro[7] = int.Parse(Dot2WTextBox.Text);
				}
				if (currentSaveChannelComboBox.SelectedIndex > 1)
				{
					currentValuePro[8] = int.Parse(Dot3RTextBox.Text);
					currentValuePro[9] = int.Parse(Dot3GTextBox.Text);
					currentValuePro[10] = int.Parse(Dot3BTextBox.Text);
					currentValuePro[11] = int.Parse(Dot3WTextBox.Text);
				}
				if (currentSaveChannelComboBox.SelectedIndex > 2)
				{
					currentValuePro[12] = int.Parse(Dot4RTextBox.Text);
					currentValuePro[13] = int.Parse(Dot4GTextBox.Text);
					currentValuePro[14] = int.Parse(Dot4BTextBox.Text);
					currentValuePro[15] = int.Parse(Dot4WTextBox.Text);
				}
				if (currentSaveChannelComboBox.SelectedIndex > 3)
				{
					currentValuePro[16] = int.Parse(Dot5RTextBox.Text);
					currentValuePro[17] = int.Parse(Dot5GTextBox.Text);
					currentValuePro[18] = int.Parse(Dot5BTextBox.Text);
					currentValuePro[19] = int.Parse(Dot5WTextBox.Text);
				}
				if (currentSaveChannelComboBox.SelectedIndex > 4)
				{
					currentValuePro[20] = int.Parse(Dot6RTextBox.Text);
					currentValuePro[21] = int.Parse(Dot6GTextBox.Text);
					currentValuePro[22] = int.Parse(Dot6BTextBox.Text);
					currentValuePro[23] = int.Parse(Dot6WTextBox.Text);
				}

				byte[] cmdData = new byte[256];

				cmdData[2] = 0x32;

				int channelNum = int.Parse(currentSaveChannelComboBox.Text);
				for (int i = 0; i < channelNum; i++)
				{
					cmdData[8 + i] = (byte)currentValuePro[i];
				}

				baseCoreNet.sendMiniCmd(devIndex, subIndex, portIndex, miniIndex, cmdData, 12 + channelNum);

				string msg = "保存完成";
				if (LanguageOper.available == true)
				{
					msg = LanguageOper.ReadValue(section, "msgStr.2");
				}
				MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.Information);
			}
			catch
			{
				string msg = "电流参数解析错误, 应为 [0,100] 之间的整数";
				if (LanguageOper.available == true)
				{
					msg = LanguageOper.ReadValue(section, "msgStr.3");
				}
				
				MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.Information);
				
				return;
			}
		}

		// 关闭窗体
		private void closeButton_Click(object sender, EventArgs e)
		{
			this.Close();
		}
	}
}
